package com.bistu.xiong.factory.data.helper;

import com.bistu.xiong.common.Common;
import com.bistu.xiong.common.app.Application;
import com.bistu.xiong.factory.R;
import com.bistu.xiong.factory.bean.ChatMessage;
import com.bistu.xiong.factory.data.DataSource;
import com.bistu.xiong.factory.data.RspCallback;
import com.bistu.xiong.factory.model.api.RspModel;
import com.bistu.xiong.factory.model.api.account.AccountRspModel;
import com.bistu.xiong.factory.model.api.account.LoginModel;
import com.bistu.xiong.factory.model.api.account.RegisterModel;
import com.bistu.xiong.factory.model.api.account.UpdateUserModel;
import com.bistu.xiong.factory.model.api.tuling.TuLingSendModel;
import com.bistu.xiong.factory.net.Network;
import com.bistu.xiong.factory.net.RemoteService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by XiongXiaoJun on 2017/12/15.
 * 账户API网络请求帮助类
 */

public class TuLingHelper {
    /**
     * 发送消息的接口，异步的调用
     * @param model    传递一个注册的Model进来
     * @param callback 成功与失败的接口回送
     */
    public static void sendMsgToTuLing(TuLingSendModel model, final DataSource.Callback<ChatMessage> callback){
        // 调用Retrofit对我们的网络请求接口做代理
        RemoteService service = Network.remoteTuLing();
        // 得到一个Call
        Call<ChatMessage> call = service.sendToRobotMessage(Common.Constance.API_KEY,model.getInfo(),model.getUserid());
        // 异步的请求
        call.enqueue(new Callback<ChatMessage>() {
            @Override
            public void onResponse(Call<ChatMessage> call, Response<ChatMessage> response) {
                ChatMessage chatMessage = response.body();
                if (chatMessage!=null && chatMessage.success()){
                    chatMessage.setRobot(true);
                    callback.onDataLoaded(chatMessage);
                }else{
                    if (chatMessage!=null)
                        Application.showToast(chatMessage.getText());
                    else
                        Application.showToast(R.string.data_network_error);
                }
            }

            @Override
            public void onFailure(Call<ChatMessage> call, Throwable t) {
                callback.onDataNotAvailable(R.string.data_network_error);
            }
        });
    }

}
